const fs = require('fs')
const {
  showAvatar
} = require("../service/user.service");
const userService = require("../service/user.service");


class UserController {
  async create(ctx, next) {
    // 获取用户传递过来的信息
    const user = ctx.request.body;
    // 将user信息存储到数据库
    const result = await userService.create(user);
    ctx.body = {
      code: 200,
      message: "请求成功",
      result: result,
    };
  }

  async showAvatar(ctx, next) {
    const {
      userId
    } = ctx.params
    const result = await showAvatar(userId)
    const {
      filename,
      mimeType
    } = result
    ctx.type = mimeType
    ctx.body = fs.createReadStream(`./uploads/${filename}`) 
  }
}

module.exports = new UserController();